<?php
namespace app\center\controller;
use app\center\logic\RemarkLogic;

/**
 * 备忘录api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/07
 * Time: 16:16
 */
class Remark extends Base {

    public function __construct(){
        parent::__construct();
        $this->r_logic = new RemarkLogic();
    }

    /**
     * 获取社区巡店联动
     * @return \think\response\Json
     */
    public function ajax_get_cp_linkage(){
        $list = [];
        $maps = [];
        $b_list = model('branch')->get_list($maps,'id,name');
        foreach($b_list as $k_1 => $v_1){
            $list[$k_1]['id'] = $v_1['id'];
            $list[$k_1]['name'] = $v_1['name'];

            $b_children = [];
            $f_maps = [];
            $f_maps[] = ['branchid','=',$v_1['id']];
            $f_list = model('floor')->get_list($f_maps,'id,name');
            foreach($f_list as $k_2 => $v_2){
                $b_children[$k_2]['id'] = $v_2['id'];
                $b_children[$k_2]['name'] = $v_2['name'];

                $f_children = [];
                $f_maps = [];
                $f_maps[] = ['floorid','=',$v_2['id']];
                $r_list = model('region')->get_list($f_maps,'id,name');
                foreach($r_list as $k_3 => $v_3){
                    $f_children[$k_3]['id'] = $v_3['id'];
                    $f_children[$k_3]['name'] = $v_3['name'];
                }
                $b_children[$k_2]['children'] = $f_children;
            }
            $list[$k_1]['children'] = $b_children;
        }

        $data['list'] = $list;
        return json($this->r_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取备注
     * @return \think\response\Json
     */
    public function ajax_get_remark(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->r_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = $this->r_logic->get_search_remark_maps($u_detail['openid'],$params['branchid'],$params['floorid'],$params['regionid'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('cp_region_ws')->get_list($maps,'id,branchid,floorid,regionid,wsid,remark,c_time','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //查找工位
            $ws_maps = [];
            $ws_maps[] = ['id','=',$v['wsid']];
            $ws_detail = model('work_station')->get_detail($ws_maps,'id,number');

            //查找区域
            $r_maps = [];
            $r_maps[] = ['id','=',$v['regionid']];
            $r_detail = model('region')->get_detail($r_maps,'id,name');

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$v['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,name');

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$v['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');

            $list[$k]['name'] = $b_detail['name'] . '-' . $f_detail['name'] . '-' . $r_detail['name'] . '-' . $ws_detail['number'];
        }

        $data['list'] = $list;
        $data['page'] = $params['page'];
        return json($this->r_logic->get_response(1,'OK',$data));
    }
}
